package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Binder;
import android.os.Bundle;
import android.widget.Button;
import android.widget.*;

import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Date;
import java.lang.*;

import android.content.Context;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.DatagramPacket;

import android.view.View;

import android.util.*;
import android.app.*;
import android.content.Intent;
import android.os.IBinder;
class g_para
{
    public  static  logClass l;
}

public class MainActivity extends AppCompatActivity {

    public LinkedList<String> log = new LinkedList<String>();

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView ed = (TextView) findViewById(R.id.textView5);
        String info = "serverIp  | serverPort\nkey | localPort\n" +
                "portNum | maxNewPortPerSecond\n" + "ahead | packSize\n" +
                "increaseDose | decreaseDose\nspeedlimit\n " +
                "logFormat:sockNum,got,lost,downloadSpeed,uploadSpeed,\n" +
                "biggestSend,biggestPeerRec,biggestPeerSend,biggestRec";
        ed.setText(info);

        try {
            File path = getBaseContext().getFilesDir();
            File file = new File(path, "config.txt");
            FileInputStream in = new FileInputStream(file);
            byte[] bytes = new byte[10000];
            in.read(bytes, 0, 10000);
            in.close();
            structWrapper re = new structWrapper(bytes);
            EditText ee;

            ee = (EditText) findViewById(R.id.port_in);
            ee.setText(""+re.readDWord());
            ee = (EditText) findViewById(R.id.local_in);
            ee.setText(""+re.readDWord());
            ee = (EditText) findViewById(R.id.num_in);
            ee.setText(""+re.readDWord());
            ee = (EditText) findViewById(R.id.idose_in);
            ee.setText(""+re.readDWord());
            ee = (EditText) findViewById(R.id.ddose_in);
            ee.setText(""+re.readDWord());
            ee = (EditText) findViewById(R.id.ahead_in);
            ee.setText(""+re.readDWord());
            ee = (EditText) findViewById(R.id.size_in);
            ee.setText(""+re.readDWord());
            ee = (EditText) findViewById(R.id.limit_in);
            ee.setText(""+re.readDWord());
            ee = (EditText) findViewById(R.id.speed_in);
            ee.setText(""+re.readDWord());
            ee = (EditText) findViewById(R.id.ip_in);
            byte[] hh = re.readArr(re.readDWord());
            String jj = new String(hh, "UTF-8");
            ee.setText(jj);
            ee = (EditText) findViewById(R.id.key_in);
            hh = re.readArr(re.readDWord());
            jj = new String(hh, "UTF-8");
            ee.setText(jj);

        } catch (Exception e) {
            ed.setText(e.toString());
        }


        MainActivity chu = this;
        Button button = (Button) findViewById(R.id.button9);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((Button) findViewById(R.id.button9)).setEnabled(false);
                EditText tt;
                tt = (EditText) findViewById(R.id.ip_in);
                String serverip = tt.getText().toString();
                tt = (EditText) findViewById(R.id.port_in);
                int serverport = Integer.parseInt(tt.getText().toString());
                tt = (EditText) findViewById(R.id.key_in);
                String key = tt.getText().toString();
                tt = (EditText) findViewById(R.id.local_in);
                int localPort = Integer.parseInt(tt.getText().toString());
                tt = (EditText) findViewById(R.id.num_in);
                int portNum = Integer.parseInt(tt.getText().toString());
                tt = (EditText) findViewById(R.id.idose_in);
                int increaseDose = Integer.parseInt(tt.getText().toString());
                tt = (EditText) findViewById(R.id.ddose_in);
                int deDose = Integer.parseInt(tt.getText().toString());
                tt = (EditText) findViewById(R.id.ahead_in);
                int pushA = Integer.parseInt(tt.getText().toString());
                tt = (EditText) findViewById(R.id.size_in);
                int sizeLi = Integer.parseInt(tt.getText().toString());
                tt = (EditText) findViewById(R.id.limit_in);
                int limit = Integer.parseInt(tt.getText().toString());
                tt = (EditText) findViewById(R.id.speed_in);
                int speed = Integer.parseInt(tt.getText().toString());
                logClass ll = new logClass(chu, serverip, serverport, key, localPort,
                        portNum, increaseDose, deDose, pushA, sizeLi, limit, speed);

            ll.setDaemon(true);
                g_para.l = ll;
                Intent it = new Intent(MainActivity.this,myServ.class);
                startService(it);

                //    ll.start();
                try {
                    File path = getBaseContext().getFilesDir();
                    File file = new File(path, "config.txt");
                    FileOutputStream stream = new FileOutputStream(file);
                    structWrapper re = new structWrapper(10000);
                    re.writeDWord(serverport);
                    re.writeDWord(localPort);
                    re.writeDWord(portNum);
                    re.writeDWord(increaseDose);
                    re.writeDWord(deDose);
                    re.writeDWord(pushA);
                    re.writeDWord(sizeLi);
                    re.writeDWord(limit);
                    re.writeDWord(speed);
                    re.writeDWord(serverip.length());
                    re.writeArr(serverip.getBytes());
                    re.writeDWord(key.length());
                    re.writeArr(key.getBytes());
                    stream.write(re.getWriteData());
                    stream.close();
                } catch (Exception e) {
                    TextView e1 = (TextView) findViewById(R.id.textView5);
                    e1.setText(e.toString());
                }
            }
        });
        button = (Button) findViewById(R.id.button8);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.exit(0);
            }
        });

    }

    void setLog2(String st) {
        log.add(st);
        if (log.size() > 18) log.removeFirst();
        String t = "";
        for (String a : log) {
            t += a + "\n";
        }
        TextView e1 = (TextView) findViewById(R.id.textView5);
        e1.setText(t);
    }
}


class logClass  extends  Thread
{
    MainActivity upper;
    String serverip;
    int serverport;
    String key2;
    int localPort;
    int portNum;
    int increaseDose;
    int deDose;
    int pushA;
    int sizeLi;
    int limit;
    int speed;

    public logClass() {
    }

    public logClass(MainActivity a, String serveripa, int serverporta, String key2a, int localPorta,
                    int portNuma, int increaseDosea, int deDosea, int pushAa, int sizeLia, int limita, int speeda) {
        upper = a;
        serverip = serveripa;
        serverport = serverporta;
        key2 = key2a;
        localPort = localPorta;
        portNum = portNuma;
        increaseDose = increaseDosea;
        deDose = deDosea;
        pushA = pushAa;
        sizeLi = sizeLia;
        limit = limita;
        speed = speeda;
    }
    synchronized void setLog(String st) {
        upper.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                upper.setLog2(st);
                helpFunc.print(st);
            }
        });
    }
    public void run() {
        try {
            touc.work(serverip, serverport, key2, localPort,
                    portNum, increaseDose, deDose, pushA, sizeLi, limit, speed, this);

        } catch (Exception e) {

        }
    }
}